package cn.wolfcode.rbac.web.controller;


import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.query.EmployeeQueryObject;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.service.IRoleService;
import cn.wolfcode.rbac.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService service;
    @Autowired
    private IDepartmentService deptService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"员工查询", "employee:list"}, logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo result = service.query(qo);
        List<Department> depts = deptService.listAll();
        model.addAttribute("depts", depts);
        model.addAttribute("result", result);
        return "employee/list";
    }

    @RequestMapping("/input")
    @RequiresPermissions(value = {"员工编辑", "employee:input"}, logical = Logical.OR)
    public String input(Model model, Long id) {
        if (id != null) {
            Employee entity = service.selectOne(id);
            model.addAttribute("entity", entity);
        }
        List<Department> depts = deptService.listAll();
        model.addAttribute("depts", depts);
        List<Role> roles = roleService.listAll();
        model.addAttribute("roles", roles);
        return "employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"员工更新", "employee:saveOrUpdate"}, logical = Logical.OR)
    public String list(Model model, Employee u, int[] ids) {
        if (u.getAdmin() == null) {
            u.setAdmin(false);
        }
        service.saveOrUpdate(u, ids);
        return "redirect:/employee/list.do";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"员工删除", "employee:delete"}, logical = Logical.OR)
    public String list(Long id) {
        service.delete(id);
        return "redirect:/employee/list.do";
    }

    @RequestMapping("/deleteMany")
    @ResponseBody
    @RequiresPermissions(value = {"员工批量删除", "employee:deleteMany"}, logical = Logical.OR)
    public JsonResult list(Long[] ids) {
        try {
            System.out.println(service);
            service.deleteMany(ids);
            return new JsonResult("成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(e.getMessage(), false);
        }
    }

    @RequestMapping("/checkname")
    @ResponseBody
    public boolean checkname(String name) {
        Employee employee = service.selectByName(name);
        return employee == null;
    }

    @RequestMapping("/exportXls")
    @ResponseBody
    public void exportXls(HttpServletResponse response) throws IOException {
        Workbook wb = service.exportXls();
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        wb.write(response.getOutputStream());
    }
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws IOException {
        service.importXls(file);
        return "redirect:/employee/list.do";
    }
}

